package javax.per.toolkit.mail;

import java.io.File;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

/**
 * 
 * @author yl34569
 *
 */
public class MailSender {
	
	
	public static boolean sendTextMail(MailSendInfo mailInfo){
		try{
		MailAuthenticator authenticator=null;
		Properties pro=mailInfo.getProperties();
		if(mailInfo.isValidate()){
			authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());   
		}
		Session sendMailSession = Session.getDefaultInstance(pro, authenticator);
		Message mailMessage = new MimeMessage(sendMailSession);
		Address from = new InternetAddress(mailInfo.getFromAddress());
		mailMessage.setFrom(from);
		
		Object[] tos=mailInfo.getToAddress();
		for(Object to:tos){
			Address toa = new InternetAddress((String)to);
			mailMessage.addRecipient(Message.RecipientType.TO,toa);
		}
		
		Object[] ccs=mailInfo.getCcAddress();
		for(Object cc:ccs){
			Address cca = new InternetAddress((String)cc);
			mailMessage.addRecipient(Message.RecipientType.CC,cca);
		}
		
		mailMessage.setSubject(mailInfo.getSubject());
		mailMessage.setSentDate(new Date());   
		String mailContent = mailInfo.getContent();
		mailMessage.setText(mailContent);
		Transport.send(mailMessage);
		return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	
	public static boolean sendHtmlMail(MailSendInfo mailInfo){
		try{
			MailAuthenticator authenticator = null;  
			Properties pro = mailInfo.getProperties();  
			if (mailInfo.isValidate()) { 
				authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());  
			}
			 Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
			 Message mailMessage = new MimeMessage(sendMailSession);   
			 Address from = new InternetAddress(mailInfo.getFromAddress());   
			 mailMessage.setFrom(from);
			 Object[] tos=mailInfo.getToAddress();
			for(Object to:tos){
				Address toa = new InternetAddress((String)to);
				mailMessage.addRecipient(Message.RecipientType.TO,toa);
			}

			Object[] ccs=mailInfo.getCcAddress();
			for(Object cc:ccs){
				Address cca = new InternetAddress((String)cc);
				mailMessage.addRecipient(Message.RecipientType.CC,cca);
			}
			
			mailMessage.setSubject(mailInfo.getSubject());   
			mailMessage.setSentDate(new Date());   
			Multipart mainPart = new MimeMultipart();   
			BodyPart html = new MimeBodyPart();   
			html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");   
			mainPart.addBodyPart(html);   
			mailMessage.setContent(mainPart);   
			Transport.send(mailMessage);   
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	
	@SuppressWarnings("restriction")
	public static boolean sendTextMailAttachedFiles(MailSendInfo mailInfo){
		try{
			MailAuthenticator authenticator = null;  
			Properties pro = mailInfo.getProperties();  
			if (mailInfo.isValidate()) { 
				authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());  
			}
			 Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
			 Message mailMessage = new MimeMessage(sendMailSession);   
			 Address from = new InternetAddress(mailInfo.getFromAddress());   
			 mailMessage.setFrom(from);
			 Object[] tos=mailInfo.getToAddress();
			for(Object to:tos){
				Address toa = new InternetAddress((String)to);
				mailMessage.addRecipient(Message.RecipientType.TO,toa);
			}

			Object[] ccs=mailInfo.getCcAddress();
			for(Object cc:ccs){
				Address cca = new InternetAddress((String)cc);
				mailMessage.addRecipient(Message.RecipientType.CC,cca);
			}
			
			mailMessage.setSubject(mailInfo.getSubject());   
			mailMessage.setSentDate(new Date());   
			
			Multipart mainPart = new MimeMultipart();   
			BodyPart contentPart = new MimeBodyPart();   
			contentPart.setText(mailInfo.getContent());
			mainPart.addBodyPart(contentPart);   
			
			Object[] attachedFiles=mailInfo.getAttachFileNames();
			for(Object o:attachedFiles){
				String fileName =new File((String)o).getName();
				BodyPart messageBodyPart= new MimeBodyPart();
				DataSource source = new FileDataSource((String)o);
				messageBodyPart.setDataHandler(new DataHandler(source));
				sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
				messageBodyPart.setFileName("=?GBK?B?"+enc.encode(fileName.getBytes(System.getProperty("sun.jnu.encoding")))+"?=");
				mainPart.addBodyPart(messageBodyPart);
			}
			
			mailMessage.setContent(mainPart);   
			mailMessage.saveChanges();
			Transport.send(mailMessage);   
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	
	@SuppressWarnings("restriction")
	public static boolean sendHtmlMailAttachedFiles(MailSendInfo mailInfo){
		try{
			MailAuthenticator authenticator = null;  
			Properties pro = mailInfo.getProperties();  
			if (mailInfo.isValidate()) { 
				authenticator = new MailAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());  
			}
			 Session sendMailSession = Session.getDefaultInstance(pro,authenticator);   
			 Message mailMessage = new MimeMessage(sendMailSession);   
			 Address from = new InternetAddress(mailInfo.getFromAddress());   
			 mailMessage.setFrom(from);
			 Object[] tos=mailInfo.getToAddress();
			for(Object to:tos){
				Address toa = new InternetAddress((String)to);
				mailMessage.addRecipient(Message.RecipientType.TO,toa);
			}

			Object[] ccs=mailInfo.getCcAddress();
			for(Object cc:ccs){
				Address cca = new InternetAddress((String)cc);
				mailMessage.addRecipient(Message.RecipientType.CC,cca);
			}
			
			mailMessage.setSubject(mailInfo.getSubject());   
			mailMessage.setSentDate(new Date());   
			Multipart mainPart = new MimeMultipart();   
			BodyPart contentPart = new MimeBodyPart();   
			contentPart.setContent(mailInfo.getContent(), "text/html; charset=utf-8");   
			mainPart.addBodyPart(contentPart);   
			
			Object[] attachedFiles=mailInfo.getAttachFileNames();
			for(Object o:attachedFiles){
				String fileName =new File((String)o).getName();
				BodyPart messageBodyPart= new MimeBodyPart();
				DataSource source = new FileDataSource((String)o);
				messageBodyPart.setDataHandler(new DataHandler(source));
				sun.misc.BASE64Encoder enc = new sun.misc.BASE64Encoder();
				messageBodyPart.setFileName("=?GBK?B?"+enc.encode(fileName.getBytes(System.getProperty("sun.jnu.encoding")))+"?=");
				mainPart.addBodyPart(messageBodyPart);
			}
			
			mailMessage.setContent(mainPart);   
			mailMessage.saveChanges();
			Transport.send(mailMessage);   
			return true;
		}catch(Exception e){
			e.printStackTrace();
		}
		return false;
	}
	
	
	/**
	 * Example for using this class.
	 * @param args command line.
	 */
	public static void main(String[] args){

		/*
		 * Test for sending mails.
		 */
		
		MailSendInfo info=new MailSendInfo();
		info.setMailServerHost("smtp.163.com");
		info.setValidate(true);
		info.setPassword("wozai2011");
		info.setUserName("fasdjfdaslsss@163.com");
		info.setFromAddress("fasdjfdaslsss@163.com");
		info.addCcAddress("L804452971@163.com");
		info.addToAddress("louyaojia@126.com");
		info.addToAddress("804452971@qq.com");
		info.setSubject("主题");   
		info.setContent("内容");   
		//info.addAttachFileNames("F:\\aaa\\新建文本文档.txt");
		//info.addAttachFileNames("F:\\aaa\\新建文本文档.txt");
		//MailOperator.sendTextMail(info);
		MailSender.sendTextMail(info);
	}
	
	
	
	
}
